﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CH.Study.ConsoleApp.ExpressionTree
{
    public abstract class BaseEntity : INotifyEntityChanged
    {

        public Dictionary<string, object> ChangedProperties { get; set; }
        public event PropertyChangedHandler PropertyChanged;
        public BaseEntity()
        {
            ChangedProperties = new Dictionary<string, object>();
            PropertyChanged+=new PropertyChangedHandler(BaseEntity_PropertyChanged);
            
        }
        private void BaseEntity_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender == this)
            {
                lock (this.ChangedProperties)
                {
                    if (this.ChangedProperties.ContainsKey(e.PropertyName))
                    {
                        this.ChangedProperties[e.PropertyName] = e.NewValue;
                    }
                    else
                    {
                        this.ChangedProperties.Add(e.PropertyName, e.NewValue);
                    }
                }

            }
        }
        protected void OnPropertyChanged(string propertyName, object oldVal, object newVal) 
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs()
            {
                PropertyName = propertyName,
                NewValue = newVal,
                OldValue = oldVal
            });
        }

      
    }
}
